﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Kugar.UI.WinForm
{
    public partial class ButtonWithDropdownList : UserControl
    {
        const string hideListText = "▲";
        const string showListText = "▼";
        

        private bool isExpand = false;

        public ButtonWithDropdownList()
        {
            InitializeComponent();
        }

        private ContextMenuStrip _buttonList = null;

        [Description(@"按钮的下拉菜单")]
        [Browsable(true)]
        public ContextMenuStrip ButtonList
        {
            get { return _buttonList; }
            set 
            {
                btnShowList.Enabled = false;

                if (_buttonList!=null)
                {
                    _buttonList.Opened -= ButtonList_Opened;
                    _buttonList.Closed -= ButtonList_Closed;

                }

                _buttonList = value;

                if (value!=null)
                {
                    btnShowList.Enabled = true;
                    value.AutoClose =this. AutoClose;
                    _buttonList.Opened += ButtonList_Opened;
                    _buttonList.Closed += ButtonList_Closed;
                    _buttonList.ShowImageMargin = false;
                    //_buttonList.TopLevel = true;
                }
            }
        }

        [Description(@"不使用该属性")]
        [Browsable(false)]
        public override ContextMenu ContextMenu
        {
            get
            {
                return base.ContextMenu;
            }
            set
            {
                //base.ContextMenu = value;
            }
        }

        [Description(@"不使用该属性")]
        [Browsable(false)]
        public override ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return base.ContextMenuStrip;
            }
            set
            {
                //base.ContextMenuStrip = value;
            }
        }

        private bool _autoClose = true;
        public bool AutoClose
        {
            get { return _autoClose; }
            set
            {
                if (_buttonList!=null)
                {
                    _buttonList.AutoClose = value;
                }

                _autoClose = value;
            }
        }

        private string _text = string.Empty;

        [Browsable(true)]
        public override string Text
        {
            set
            {
                    _text = value;
                    btnClickButton.Text = value;
            }
            get { return _text; }
        }

        public bool Expand
        {
            get { return isExpand; }
            set
            {
                expandList(value);

                //if (value==isExpand)
                //{
                //    return;
                //}

                //if (value==true && isExpand==false)
                //{
                //    expandList(true);
                //}
                //else if(!value  && isExpand==true)
                //{
                //    expandList(false);
                //}
            }
        }

        public event EventHandler MainButtonClick;

        public event EventHandler ButtonListOpened;
        public event EventHandler ButtonListClosed;

        private void ButtonList_Opened(object sender, EventArgs e)
        {
            isExpand = true;

            btnShowList.Text = hideListText;


            if (ButtonListOpened!=null)
            {
                ButtonListOpened(this, e);
            }
        }

        private void ButtonList_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            isExpand = false;

            btnShowList.Text = showListText;

            if (ButtonListClosed != null)
            {
                ButtonListClosed(this, e);
            }
          
            //if (ButtonListOpened != null)
            //{
            //    ButtonListOpened(this, e);
            //}
        }

        private void expandList(bool isShow)
        {
            btnShowList.Enabled = false;

            if (_buttonList!=null)
            {
                _buttonList.Close();
                
                if (isShow)
                {
                    var menuWidth = _buttonList.Width;

                    _buttonList.Show(btnClickButton,this.Width-menuWidth,btnClickButton.Height);

                }
                btnShowList.Enabled = true;
            }
        }

        private void btnShowList_Click(object sender, EventArgs e)
        {

            //Expand = !isExpand;
            expandList(!isExpand);
        }

        private void btnClickButton_Click(object sender, EventArgs e)
        {
            if (MainButtonClick!=null)
            {
                MainButtonClick(this, e);
            }
        }

        ~ButtonWithDropdownList()
        {
            if (_buttonList != null)
            {
                _buttonList.Opened -= ButtonList_Opened;
                _buttonList.Closed -= ButtonList_Closed;

            }
        }
    }
}

